package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.LiveRoomInfo;
import com.ymatou.iapi.livebuyer.parameter.QueryAllLiveRoomBean;
import com.ymatou.iapi.livebuyer.service.QueryAllLiveRoomCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.YMTDateUtil;

/**
 * 查询所有的直播间信息 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QueryAllLiveRoom {
	private static QueryAllLiveRoomBean queryallliveroomBean;
	private static QueryAllLiveRoomCall queryallliveroomCall;
	private static LiveRoomWapper liveRoomWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询所有的直播间信息");
	}

	@Before
	public void caseUp() {
		queryallliveroomBean = new QueryAllLiveRoomBean();
		queryallliveroomCall = new QueryAllLiveRoomCall();
		liveRoomWapper = new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryallliveroomCall.getOpurl());
		liveRoomWapper = null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(LiveRoomInfo liveRoomInfo) {
		LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveRoomInfo.getLiveId());
		Logger.verifyEquals(liveroom.getEndTime().subSequence(0, 19), liveRoomInfo.getEndTime(), "验证EndTime");
		Logger.verifyEquals(liveroom.getStartTime().substring(0, 19), liveRoomInfo.getStartTime(), "验证StartTime");
		Logger.verifyEquals(liveroom.getGroupId(), liveRoomInfo.getGroupId(), "验证GroupId");
		// Logger.verifyEquals(liveroom.getHistoryPlayUrl(),
		// liveRoomInfo.getHistoryPlayUrl(), "验证HistoryPlayUrl");
		Logger.verifyEquals(liveroom.getLiveCover(), liveRoomInfo.getLiveCover(), "验证LiveCover");
		Logger.verifyEquals(liveroom.getId(), liveRoomInfo.getLiveId(), "验证LiveId");
		// 修改status
		if (liveroom.getLiveStatus() == 0) {
			if (liveroom.getStartTime().compareTo(YMTDateUtil.getSysTime()) < 0) {
				Logger.verifyEquals(1, liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			} else {
				Logger.verifyEquals(liveroom.getLiveStatus(), liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			}
		}

		Logger.verifyEquals(liveroom.getLiveTitle(), liveRoomInfo.getLiveTitle(), "验证LiveTitle");
		Logger.verifyEquals(liveroom.getPlayUrl(), liveRoomInfo.getPlayUrl(), "验证PlayUrl");
		if (liveRoomInfo.getPreVideo() != null) {
			Logger.verifyEquals(liveroom.getPreVideoHeight(), liveRoomInfo.getPreVideo().getPreVideoHeight(), "验证getPreVideoHeight");
			Logger.verifyEquals(liveroom.getPreVideoLength(), liveRoomInfo.getPreVideo().getPreVideoLength(), "验证getPreVideoLength");
			Logger.verifyEquals(liveroom.getPreVideoUrl(), liveRoomInfo.getPreVideo().getPreVideoUrl(), "验证getPreVideoUrl");
			Logger.verifyEquals(liveroom.getPreVideoWidth(), liveRoomInfo.getPreVideo().getPreVideoWidth(), "验证getPreVideoWidth");
		}
		Logger.verifyEquals(liveroom.getUserId(), liveRoomInfo.getUserId(), "验证UserId");
		Logger.verifyEquals(liveroom.getLiveDesc(), liveRoomInfo.getLiveDesc(), "验证LiveDesc");
		Logger.verifyEquals(liveroom.getPutawayCount(), liveRoomInfo.getPutawayCount(), "验证PutawayCount");
		Map livestatusmap = liveRoomWapper.selectLiveRoomStatById(liveRoomInfo.getLiveId());
		if (livestatusmap != null) {
			Logger.verifyEquals(livestatusmap.get("ViewRealNum"), liveRoomInfo.getRealViewNum(), liveRoomInfo.getLiveId() + ":验证ViewRealNum");
			Logger.verifyEquals(livestatusmap.get("PreViewNum"), liveRoomInfo.getPreViewNum(), liveRoomInfo.getLiveId() + ":验证PreViewNum");
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryAllLiveRoom_001() {
		Logger.start(true, "查询所有的直播间信息成功");
		try {
			int index = 1;
			int size = 10;
			queryallliveroomBean.setPageIndex(index);
			queryallliveroomBean.setPageSize(size);
			queryallliveroomCall.setData(queryallliveroomBean);
			queryallliveroomCall.callService();
			Logger.verifyEquals("200", queryallliveroomCall.getString("code"), "验证code");
			Long count = liveRoomWapper.selectCountLiveroom(null);
			Long pageCount = count / size + (count % size == 0 ? 0 : 1);
			Logger.verifyEquals(queryallliveroomCall.getTotalCount(), count.intValue(), "验证count");
			Logger.verifyEquals(queryallliveroomCall.getPageCount(), pageCount.intValue(), "验证pageCount");
			ArrayList<LiveRoomInfo> roomlist = queryallliveroomCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : roomlist) {
				verifyDB(liveRoomInfo);
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryAllLiveRoom_002() {
		Logger.start(true, "不输入pageIndex失败");
		try {
			queryallliveroomBean.setPageSize(10);
			queryallliveroomCall.setData(queryallliveroomBean);
			queryallliveroomCall.callService();
			Logger.verifyEquals("100", queryallliveroomCall.getString("code"), "验证code");
			Logger.verifyIsContains("错误的请求参数", queryallliveroomCall.getString("message"), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryAllLiveRoom_003() {
		Logger.start(true, "不输入pageSize失败");
		try {
			queryallliveroomBean.setPageIndex(1);
			queryallliveroomCall.setData(queryallliveroomBean);
			queryallliveroomCall.callService();
			Logger.verifyEquals("100", queryallliveroomCall.getString("code"), "验证code");
			Logger.verifyIsContains("错误的请求参数", queryallliveroomCall.getString("message"), "验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QueryAllLiveRoom_004() {
		Logger.start(true, "翻页查询所有的直播间信息成功");
		try {
			int index = 1;
			int size = 10;
			boolean iscontinue = true;
			Long count = liveRoomWapper.selectCountLiveroom(null);
			Long pageCount = count / size + (count % size == 0 ? 0 : 1);
			Set liveidset = new HashSet<Integer>();
			while (iscontinue) {
				queryallliveroomBean.setPageIndex(index);
				queryallliveroomBean.setPageSize(size);
				queryallliveroomCall.setData(queryallliveroomBean);
				queryallliveroomCall.callService();
				Logger.verifyEquals("200", queryallliveroomCall.getString("code"), "验证code");
				Logger.verifyEquals(queryallliveroomCall.getTotalCount(), count.intValue(), "验证count");
				Logger.verifyEquals(queryallliveroomCall.getPageCount(), pageCount.intValue(), "验证pageCount");
				ArrayList<LiveRoomInfo> roomlist = queryallliveroomCall.getLiveRoomInfo();
				for (LiveRoomInfo liveRoomInfo : roomlist) {
					Logger.verifyEquals(true, liveidset.add(liveRoomInfo.getLiveId()), "验证liveid不重复：" + liveRoomInfo.getLiveId());
				}
				// 最多翻页10页
				if (index >= pageCount || index > 10) {
					iscontinue = false;
				}
				index++;
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryAllLiveRoom_005() {
		Logger.start(true, "查询状态23的直播间信息成功");
		try {
			int index = 1;
			int size = 10;
			queryallliveroomBean.setPageIndex(index);
			queryallliveroomBean.setPageSize(size);
			queryallliveroomBean.setLiveStatus(Arrays.asList(2, 3));
			queryallliveroomCall.setData(queryallliveroomBean);
			queryallliveroomCall.callService();
			Logger.verifyEquals("200", queryallliveroomCall.getString("code"), "验证code");
			Long count = liveRoomWapper.selectCountLiveroomByStatus(2);
			Long count2 = liveRoomWapper.selectCountLiveroomByStatus(3);
			count = count + count2;
			Long pageCount = count / size + (count % size == 0 ? 0 : 1);
			Logger.verifyEquals(queryallliveroomCall.getTotalCount(), count.intValue(), "验证count");
			Logger.verifyEquals(queryallliveroomCall.getPageCount(), pageCount.intValue(), "验证pageCount");
			ArrayList<LiveRoomInfo> roomlist = queryallliveroomCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : roomlist) {
				verifyDB(liveRoomInfo);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryAllLiveRoom_006() {
		Logger.start(true, "指定买手查询所有的直播间信息成功");
		try {
			int index = 1;
			int size = 10;
			int sellerid = 3383;
			queryallliveroomBean.setPageIndex(index);
			queryallliveroomBean.setPageSize(size);
			queryallliveroomBean.setUserId(sellerid);
			queryallliveroomCall.setData(queryallliveroomBean);
			queryallliveroomCall.callService();
			Logger.verifyEquals("200", queryallliveroomCall.getString("code"), "验证code");
			ArrayList<LiveRoomInfo> roomlist = queryallliveroomCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : roomlist) {
				verifyDB(liveRoomInfo);
				Logger.verifyEquals(sellerid, liveRoomInfo.getUserId(), "验证买手");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryAllLiveRoom_007() {
		Logger.start(true, "指定标题查询所有的直播间信息成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			int index = 1;
			int size = 10;
			queryallliveroomBean.setPageIndex(index);
			queryallliveroomBean.setPageSize(size);
			queryallliveroomBean.setLiveTitle(livedto.getLiveTitle());
			queryallliveroomCall.setData(queryallliveroomBean);
			queryallliveroomCall.callService();
			Logger.verifyEquals("200", queryallliveroomCall.getString("code"), "验证code");
			ArrayList<LiveRoomInfo> roomlist = queryallliveroomCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : roomlist) {
				verifyDB(liveRoomInfo);
				Logger.verifyIsContains(livedto.getLiveTitle(), liveRoomInfo.getLiveTitle(), "验证标题包含");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryAllLiveRoom_008() {
		Logger.start(true, "指定id查询所有的直播间信息成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			int index = 1;
			int size = 10;
			queryallliveroomBean.setPageIndex(index);
			queryallliveroomBean.setPageSize(size);
			queryallliveroomBean.setLiveId(livedto.getId());
			queryallliveroomCall.setData(queryallliveroomBean);
			queryallliveroomCall.callService();
			Logger.verifyEquals("200", queryallliveroomCall.getString("code"), "验证code");
			ArrayList<LiveRoomInfo> roomlist = queryallliveroomCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : roomlist) {
				verifyDB(liveRoomInfo);
				Logger.verifyEquals(livedto.getId(), liveRoomInfo.getLiveId(), "验证id");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}